/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nhadat.dao;

import java.util.List;
import nhadat.entity.TblKhuVuc;
import nhadat.hibernate.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Xe Bay + DUNGND60190
 */
public class KhuVucDAO {

    private Session session;

    public KhuVucDAO() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

//    public static void main(String[] args) {
//        KhuVucDAO test = new KhuVucDAO();
//        String makhuvuc = test.getMakhuvucByQuanHuyenTinhThanh("TP. Hồ Chí Minh", "Quận 1");
//        System.out.println(makhuvuc);
//    }
    public TblKhuVuc[] getAllKhuVuc() {
        Transaction trans = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            trans = session.getTransaction();
            trans.begin();
            String sql = "from TblKhuVuc";
            Query query = session.createQuery(sql);

            List list = query.list();
            TblKhuVuc[] dskhuvuc = new TblKhuVuc[list.size()];
            session.flush();
            trans.commit();

            if (!list.isEmpty()) {
                list.toArray(dskhuvuc);
                return dskhuvuc;
            }
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;
    }

    public String getMakhuvucByQuanHuyenTinhThanh(String tinhthanh, String quanhuyen) {
        Transaction trans = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            trans = session.getTransaction();
            trans.begin();
            String sql = "select makhuvuc from TblKhuVuc where tinhthanh = ? and quanhuyen = ?";
            Query query = session.createQuery(sql);
            query.setString(0, tinhthanh);
            query.setString(1, quanhuyen);

            String makhuvuc = query.uniqueResult().toString();

            session.flush();
            trans.commit();
            if (!makhuvuc.isEmpty()) {
                return makhuvuc;
            }
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;
    }

    public String[] getAllTinhThanh() {
        Transaction trans = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            trans = session.getTransaction();
            trans.begin();
//            String sql = "From TblKhuVuc";
//            Query query = session.createQuery(sql);
//
//            List result = query.list();
//
//            TblKhuVuc[] dsKhuVuc = new TblKhuVuc[result.size()];
//            result.toArray(dsKhuVuc);

            Criteria criteria = (Criteria) session.createCriteria(TblKhuVuc.class);
            criteria.setProjection(Projections.distinct(Projections.property("tinhthanh")));
            List list = criteria.list();

            session.flush();
            trans.commit();

            if (!list.isEmpty()) {
                String[] dsTinhThanh = new String[list.size()];

                list.toArray(dsTinhThanh);

                return dsTinhThanh;
            }
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            // e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;
    }

    public String[] getAllQuanHuyenByTinhThanh(String tinhthanh) {
        Transaction trans = null;
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            trans = session.getTransaction();
            trans.begin();
            String sql = "From TblKhuVuc Where tinhthanh = ?";
            Query query = session.createQuery(sql);
            query.setString(0, tinhthanh);

            List result = query.list();

            session.flush();
            trans.commit();

            String[] dsQuanHuyen = new String[result.size()];
            if (!result.isEmpty()) {
                for (int i = 0; i < result.size(); i++) {
                    TblKhuVuc khuvuc = (TblKhuVuc) result.get(i);
                    dsQuanHuyen[i] = khuvuc.getQuanhuyen();
                }
            }
            return dsQuanHuyen;
        } catch (Exception e) {
            if (trans.isActive()) {
                trans.rollback();
            }
            // e.printStackTrace();
            System.out.println(e.getMessage());
        }
        return null;
    }
}
